package com.yami.shop.mystery.common.listener;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yami.shop.bean.event.NewcomerRewardsEvent;
import com.yami.shop.mystery.common.model.ActivityPresell;
import com.yami.shop.mystery.common.model.ActivityProd;
import com.yami.shop.mystery.common.model.InviteRecord;
import com.yami.shop.mystery.common.service.ActivityPresellService;
import com.yami.shop.mystery.common.service.ActivityProdService;
import com.yami.shop.mystery.common.service.InviteRecordService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Slf4j
@Component("newcomerRewardsListener")
@AllArgsConstructor
public class NewcomerRewardsListener {

    @Autowired
    private ActivityPresellService activityPresellService;
    @Autowired
    private ActivityProdService prodService;
    @Autowired
    private InviteRecordService inviteRecordService;

    /**
     * 功能描述: 免费领取活动
     * @Author: LSR
     * @Date: 2023/9/5 11:20
     */
    @EventListener(NewcomerRewardsEvent.class)
    public void newcomerRewardsAdd(NewcomerRewardsEvent event) {
        // 领取发放
        if(event.getGrantType().equals("2")){
            ActivityPresell byId = activityPresellService.getById(event.getActivityId());
            if(byId.getActivityStartTime().after(new Date()) || byId.getActivityEndTime().before(new Date())){
                return;
            }
        }
        // 自动发放
        else if(event.getGrantType().equals("1")){
            // 获取活动信息
            QueryWrapper<ActivityPresell> activityPresellQueryWrapper = new QueryWrapper<>();
            activityPresellQueryWrapper.eq("activity_type",event.getActivityType());
            activityPresellQueryWrapper.eq("del_flag",0);
            activityPresellQueryWrapper.eq("status",0);
            activityPresellQueryWrapper.le("activity_end_time",new Date());
            activityPresellQueryWrapper.ge("activity_start_time",new Date());
            ActivityPresell activityPresell = activityPresellService.getOne(activityPresellQueryWrapper);
            if(ObjectUtil.isEmpty(activityPresell)){
                return;
            }
            event.setActivityId(activityPresell.getId());
        }
        // 获取商品信息
        List<ActivityProd> prods = prodService.listByActivityId(event.getActivityId());
        for(ActivityProd activityProd : prods){
            // 查询是否领取过
            QueryWrapper<InviteRecord> inviteRecordQueryWrapper = new QueryWrapper<>();
            inviteRecordQueryWrapper.eq("user_id",event.getUserId());
            inviteRecordQueryWrapper.eq("activity_id",event.getActivityId());
            inviteRecordQueryWrapper.eq("prod_id",activityProd.getProdId());
            InviteRecord one = inviteRecordService.getOne(inviteRecordQueryWrapper);
            if(ObjectUtil.isNotEmpty(one)){
                return;
            }
            // 添加数据
            InviteRecord inviteRecord = new InviteRecord();
            inviteRecord.setUserId(event.getUserId());
            inviteRecord.setProdId(activityProd.getProdId());
            inviteRecord.setActivityId(event.getActivityId());
            inviteRecord.setStatus(0L);
            inviteRecordService.save(inviteRecord);
        }
    }

}
